﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace AirplaneReservation
{
    public partial class CancelTicket : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
            ccon.Open();

            string sql = "SELECT [FirstName], [LastName], [FlightId], [ClassId], [FlightCode], [originCity], [desCity], [DepartureTime] as Departure, Convert(varchar(10),DepartureTime,101) as DepartDate, [Name] as Class, [FinalPrice] as Price, [Code], Status FROM [View_TicketStatus] where Code='" + ticketNo.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, ccon);
            DataTable dt = new DataTable();
            da.Fill(dt);

            int count = dt.Rows.Count;

            if (count != 0)
            {
                string a = dt.Rows[0]["Status"].ToString();
                string memberId = Session["ssId"].ToString();
                string flight_id = dt.Rows[0]["FlightId"].ToString();
                string class_id = dt.Rows[0]["ClassId"].ToString();
                if (a == "0")
                {
                    string codeB = "CancellingB" + flight_id + class_id + memberId;
                    string sql2 = "update Ticket set Code='" + codeB + "', Status=" + 3 + " where Code='" + ticketNo.Text + "' ";
                    SqlCommand cmd = new SqlCommand(sql2, ccon);
                    cmd.ExecuteNonQuery();
                    cancellationNo.Text = codeB;
                }
                else if (a == "1")
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();

                    string dt_Now = DateTime.Now.ToString("MM/dd/yyyy");
                    string depart = dt.Rows[0]["DepartDate"].ToString();
                    TimeSpan timespan = Convert.ToDateTime(depart).Subtract(Convert.ToDateTime(dt_Now));
                    int dayBetween = int.Parse(timespan.TotalDays.ToString());

                    if (dayBetween > 14)
                    {
                        TextBox1.Text = "30";
                        TextBox2.Text = "If your flight departure is within the next 48 hours please call the respective airline, cancel the reservation and collect a cancellation reference number. Mail these details to us by filling the cancellation form given below.\nIf your flight departure is over 48 hours you may send us the cancellation details by filling the form below. We will arrange to cancel the ticket for you with the airline and process the refund.\n If your flight is cancelled /delayed , we request you to get the cancellation stamp from the respective Airline counter on the ticket and mail the same to us at the below mentioned address or you can scan the cancelled ticket.";
                    }
                    else
                    {
                        TextBox1.Text = "50";
                        TextBox2.Text = "If your flight departure is within the next 48 hours please call the respective airline, cancel the reservation and collect a cancellation reference number. Mail these details to us by filling the cancellation form given below.\nIf your flight departure is over 48 hours you may send us the cancellation details by filling the form below. We will arrange to cancel the ticket for you with the airline and process the refund.\n If your flight is cancelled /delayed , we request you to get the cancellation stamp from the respective Airline counter on the ticket and mail the same to us at the below mentioned address or you can scan the cancelled ticket.";
                    }
                }
                else
                    cancellationNo.Text = "Wrong code. Please enter again";
            }
            else
            {
                cancellationNo.Text = "Wrong code. Please enter again";
                ccon.Close();
            }
        }

        protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[3].Visible = false;
                e.Row.Cells[8].Visible = false;
                e.Row.Cells[12].Visible = false;
                e.Row.Cells[11].Visible = false;
            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[3].Visible = false;
                e.Row.Cells[8].Visible = false;
                e.Row.Cells[12].Visible = false;
                e.Row.Cells[11].Visible = false;
            }
        }

        protected void cancelT_Click(object sender, EventArgs e)
        {
            SqlConnection ccon = new SqlConnection(ConfigurationManager.ConnectionStrings["airplaneConnectionString"].ToString());
            ccon.Open();

            string sql = "SELECT [FirstName], [LastName], [FlightId], [ClassId], [FlightCode], [originCity], [desCity], [DepartureTime] as Departure, Convert(varchar(10),DepartureTime,101) as DepartDate, [Name] as Class, [FinalPrice] as Price, [Code], Status FROM [View_TicketStatus] where Code='" + ticketNo.Text + "'";
            SqlDataAdapter da = new SqlDataAdapter(sql, ccon);
            DataTable dt = new DataTable();
            da.Fill(dt);

            int count = dt.Rows.Count;
            string a = dt.Rows[0]["Status"].ToString();
            string memberId = Session["ssId"].ToString();
            string flight_id = dt.Rows[0]["FlightId"].ToString();
            string class_id = dt.Rows[0]["ClassId"].ToString();
            string codeB2 = "CancellingC" + flight_id + class_id + memberId;
            Double price = Convert.ToDouble(dt.Rows[0]["Price"].ToString());


            string refundP = TextBox1.Text;
            int refund_Per = int.Parse(refundP);
            Double RefundPrice = refund_Per * price / 100;
            Double amount = price - RefundPrice;

            string sql3 = "update Ticket set Code='" + codeB2 + "', RefundPercent=" + refundP + ", RefundPrice=" + RefundPrice + ", Status=" + 3 + "  where Code='" + ticketNo.Text + "' ";
            SqlCommand cmd2 = new SqlCommand(sql3, ccon);
            cmd2.ExecuteNonQuery();

            string sql4 = "select Money from CreditCard where CreditCard.Code='" + Session["ssCredit"].ToString() + "'";
            SqlDataAdapter da2 = new SqlDataAdapter(sql4, ccon);
            DataTable dt2 = new DataTable();
            da2.Fill(dt2);
            int count2 = dt2.Rows.Count;
            if (count2 != 0)
            {
                Double m = Convert.ToDouble(dt2.Rows[0]["Money"].ToString());
                Double mAfter = m + amount;
                string sql5 = "update CreditCard set Money=" + mAfter + " where CreditCard.Code='" + Session["ssCredit"].ToString() + "' ";
                SqlCommand cmd5 = new SqlCommand(sql5, ccon);
                cmd5.ExecuteNonQuery();
            }
            cancellationNo.Text = codeB2;
        }

    }
}
